package com.wu.controller;


import com.wu.feign.OrderFeign;
import com.wu.pojo.Menu;
import com.wu.pojo.Order;
import com.wu.pojo.User;
import com.wu.vo.OrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderFeign orderFeign;

    @GetMapping("/save/{mid}")
    public String save(@PathVariable("mid") long mid, HttpSession session){
        User user = (User) session.getAttribute("user");
        Order order = new Order();
        Menu menu = new Menu();
        menu.setId(mid);
        order.setUser(user);
        order.setMenu(menu);
        order.setDate(new Date());
        orderFeign.save(order);
        return "redirect:/menu/redirect/order";
    }

    @GetMapping("/findallByUid")
    @ResponseBody
    public OrderVo findAllByUid(@RequestParam("page")int index,@RequestParam("limit")int limit,HttpSession session){
        User user = (User) session.getAttribute("user");
        int page = (index-1)*limit;
        return orderFeign.findAllByUid(page,limit,user.getId());
    }


    @GetMapping("/findAllByState")
    @ResponseBody
    public OrderVo findAll(@RequestParam("page")int index,@RequestParam("limit")int limit){
        int page = (index-1)*limit;
        return new OrderVo().setCode(0).setMsg("").setCount(orderFeign.count()).setData(orderFeign.findAll(page,limit));
    }

    @GetMapping("/updateState/{id}")
    public String updata(@PathVariable("id")long id){
        orderFeign.updata(id);
        return "redirect:/menu/redirect/order_handler";
    }

}
